<script setup lang="ts">
import { ref, onMounted } from 'vue'
import NavBar from '../components/NavBar.vue'
import axios from "axios";
import { useRouter } from 'vue-router'

// 定义分类导航项
// const categories = ref(['推荐', '创作', '职场', '绘画', '生活', '学习', '娱乐'])
const categories = ref([])
const activeCategory = ref()

// 定义子分类
const subCategories = ref([])
const activeSubCategory = ref()
const urlhost = "http://localhost:8082/aiades/"
const show2 = ref<Card[]>([])
const aiades = ref<Card2>({
  classifyId: null,
  createId: null,
})
interface Card2 {
  classifyId: number | null//主分类外键
  createId: number | null//子分类外键
}
// 定义卡片列表数据
interface Card {
  aidesid: number
  aidesName: string  //标题
  description: string //描述
  usertotal: number //用户使用次数
  aidesImg: string //图片地址
  classifyid: number | null//主分类外键
  createid: number | null//子分类外键
}
// 展示所有
const show = () => {
  axios.post(urlhost + "findaiades", aiades.value)
    .then(res => {
      console.log("发送的数据:", aiades.value);
      show2.value = res.data;
      console.log("接收到的数据:", res.data);
    })
    .catch(error => {
      console.error("请求错误:", error);
    });
}
// 主分类
const classifyshow = () => {
  axios.get(urlhost + "findclassify").then(res => {
    categories.value = res.data;
  })
}
// 子分类
const createshow = () => {
  axios.get(urlhost + "findcreate").then(res => {
    subCategories.value = res.data;
  })
}

// 切换分类
const switchCategory = (classifyid: string) => {
  const parsedClassifyId = parseInt(classifyid);
  aiades.value.classifyId = isNaN(parsedClassifyId) ? null : parsedClassifyId; // 确保 classifyid 是数字类型，否则设置为 null
  show(); // 调用 show 方法进行模糊查询
}
// 主类全部
const quan1 = () => {
  aiades.value.classifyId = null;
  show();
}
// 切换子分类
const switchSubCategory = (createid: string) => {
  const parsedClassifyId = parseInt(createid);
  aiades.value.createId = isNaN(parsedClassifyId) ? null : parsedClassifyId; // 确保 classifyid 是数字类型，否则设置为 null
  show(); // 调用 show 方法进行模糊查询
}
// 子类全部
const quan2 = () => {
  aiades.value.createId = null;
  show();
}

const router = useRouter()

// 点击卡片跳转到对话页面
const handleCardClick = (card: Card) => {
  console.log('点击卡片:', card) // 添加调试日志
  try {
    // 所有卡片都跳转到绘画页面
    router.push({
      path: '/draw-chat',
      query: {
        title: card.aidesName
      }
    })
  } catch (error) {
    console.error('跳转失败:', error)
  }
}

onMounted(() => {
  show()
  classifyshow()
  createshow()
});
</script>

<template>
  <div class="assistant-container">
    <NavBar />
    <!-- 主分类导航 -->
    <div class="category-nav">
      <div class="scroll-container">
        <div class="category-item" @click="quan1()">全部</div>
        <div v-for="category in categories" :key="category.classifyid" class="category-item"
          :class="{ active: activeCategory === category }" @click="switchCategory(category.classifyid)">
          {{ category.classifyname }}
        </div>
      </div>
    </div>
    <!-- 子分类导航 -->
    <div class="subcategory-nav">
      <div class="scroll-container">
        <div class="subcategory-item" @click="quan2()">全部</div>
        <div v-for="category in subCategories" :key="category.createid" class="subcategory-item"
          :class="{ active: activeSubCategory === category }" @click="switchSubCategory(category.createid)">
          {{ category.createname }}
        </div>
      </div>
    </div>
    <!-- 卡片列表 -->
    <div class="card-grid">
      <div
        v-for="card in show2"
        :key="card.aidesid"
        class="card"
        @click="handleCardClick(card)"
      >
        <div
          class="card-image"
          :style="{ backgroundImage: `url(${card.aidesImg})` }"
        >
          <div class="card-users">
            <i class="fire-icon"></i>
            {{ card.usertotal }}
          </div>
        </div>
        <div class="card-content">
          <h3>{{ card.aidesName }}</h3>
          <p>{{ card.description }}</p>
        </div>
      </div>
    </div>
  </div>

</template>

<style scoped>
.assistant-container {
  padding-top: 84px;
  height: 100vh;
  overflow-y: auto;
  background: #f5f5f5;
}

.category-nav {
  background: white;
  padding: 12px 0;
  border-bottom: 1px solid #eee;
  position: fixed;
  top: 84px;
  left: 0;
  right: 0;
  z-index: 10;
  max-width: 414px;
  margin: 0 auto;
}

.subcategory-nav {
  background: white;
  padding: 10px 0;
  border-bottom: 1px solid #eee;
  position: fixed;
  top: 128px;
  left: 0;
  right: 0;
  z-index: 10;
  max-width: 414px;
  margin: 0 auto;
}

.scroll-container {
  display: flex;
  overflow-x: auto;
  padding: 0 16px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.scroll-container::-webkit-scrollbar {
  display: none;
}

.category-item {
  padding: 6px 16px;
  margin-right: 16px;
  color: #666;
  font-size: 15px;
  white-space: nowrap;
  cursor: pointer;
}

.category-item.active {
  color: #007AFF;
  font-weight: 500;
  position: relative;
}

.category-item.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 16px;
  right: 16px;
  height: 2px;
  background: #007AFF;
  border-radius: 1px;
}

.subcategory-item {
  padding: 4px 12px;
  margin-right: 8px;
  color: #666;
  font-size: 13px;
  background: #f5f5f5;
  border-radius: 16px;
  white-space: nowrap;
  cursor: pointer;
}

.subcategory-item.active {
  color: white;
  background: #007AFF;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 12px;
  margin-top: 132px;
}

.card {
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card:active {
  transform: translateY(0);
}

.card-image {
  height: 160px;
  background-size: cover;
  background-position: center;
  position: relative;
}

.card-users {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.fire-icon {
  width: 12px;
  height: 12px;
  background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="%23fff" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z"/></svg>');
  background-size: contain;
}

.card-content {
  padding: 12px;
}

.card-content h3 {
  margin: 0;
  font-size: 15px;
  color: #333;
  margin-bottom: 4px;
}

.card-content p {
  margin: 0;
  font-size: 12px;
  color: #666;
  line-height: 1.4;
}
</style>